<%@ page language="java" import="org.checkerframework.checker.units.qual.A" pageEncoding="UTF-8" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.conn.RecordSetDataSource" %>
<%@ page import="weaver.formmode.setup.ModeRightInfo" %>
<%@ page import="weaver.general.TimeUtil" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Set" %>
<%@ page import="java.util.UUID" %>
<%--最新比价信息--%>
<%
    User user = HrmUserVarify.getUser(request, response);
    int userid = user.getUID();
    int formmodeid= 156;
    RecordSetDataSource dataSource = new RecordSetDataSource( "xfcs" );
    out.print( ">>>>>>>>开始执行>>>>>>"+"<br/>");
    try {
        String guid = getGuid();
        out.print( ">>>>>>>>guid："+guid+"<br/>");
        dataSource.executeSql(
                "select t1.CategoryGuid,t1.BiJiaGuid,t1.MingCheng,t1.PinPai+'-'+t1.XingHao+'-'+t1.GuiGe as wpmc," +
                "t1.Unit,t1.Business_BaseinfoName,t1.Price,CONVERT(varchar(100),t.OperateDate, 23) as OperateDate\n" +
                "from T_Assct_BiJia t,T_Assct_BiJiaDetail t1 where t.RowGuid=t1.BiJiaGuid \n" +
                "and t.ProcessVersionInstanceGuid in ("+guid+")");
        int count = 0;
        int counts = dataSource.getCounts();
        out.print( ">>>>>>>>查询记录总数为："+counts+"<br/>");
        while (dataSource.next()){
            Map dataMap = new HashMap(  );
            String BiJiaGuid = Util.null2String(dataSource.getString("BiJiaGuid"));
            String CategoryGuid = Util.null2String(dataSource.getString("CategoryGuid"));
            String bjdwxx = getBjdwxx(CategoryGuid,BiJiaGuid);
            dataMap.put("wplb",Util.null2String(dataSource.getString("MingCheng")));//物品类别
            dataMap.put("dw",Util.null2String(dataSource.getString("Unit")));//单位
            dataMap.put("wpmc",Util.null2String(dataSource.getString("wpmc")));//物品名称
            dataMap.put("bjdwxx",Util.null2String(bjdwxx));//比价单位信息
            dataMap.put("cysj",Util.null2String(dataSource.getString("Business_BaseinfoName")));//采用商家
            dataMap.put("cydj",Util.null2String(dataSource.getString("Price")));//采用单价
            dataMap.put("bjsj",Util.null2String(dataSource.getString("OperateDate")));//比价时间
            int dataId = insertModelData(formmodeid,userid,dataMap);
            if(dataId>0){
                count++;
                out.print( dataId+">>>>>执行成功主表数据记录数：>>>"+count+",主表数据总记录数：>>>"+counts);
            }
        }

    }catch (Exception e){
        out.print("执行异常，请查看日志！！！");
    }
%>
<%!
    //将数据插入表单建模
    public static int insertModelData(int modeid, int userid, Map map){
        int dataid=0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        //查询该模块表名
        String sql = "select b.tablename,a.formid from modeinfo a left join workflow_bill b on a.formid=b.id where a.id=?";
        rs.executeQuery(sql,modeid);
        rs.next();
        String tablename= Util.null2String(rs.getString("tablename"));
        String uuid = UUID.randomUUID().toString();
        String insSql = "insert into "+tablename
                +"(uuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values(?,?,?,?,?)";
        Object[] params = {uuid, userid, TimeUtil.getCurrentDateString(), TimeUtil.getOnlyCurrentTimeString(), modeid};
        if(rs.executeUpdate(insSql,params)){
            rs1.executeQuery("select id from "+tablename+" where uuid=?",uuid);
            rs1.next();
            dataid= Util.getIntValue(rs1.getString("id"));
            if(dataid>0){
                //遍历数据 进行update
                String updatesql="update "+tablename+" set ";
                Set<String> keySet = map.keySet();
                for(String key : keySet){
                    updatesql+=key+"='"+map.get(key).toString()+"',";
                }
                if(updatesql.endsWith(",")){
                    updatesql=updatesql.substring(0, updatesql.length()-1);
                    updatesql+=" where id="+dataid;
                    rs.executeUpdate(updatesql);
                }
                //进行权限重构
                getNewModeRight(userid, modeid, dataid);
            }
        }
        return dataid;
    }
    //建模权限重构
    public static void getNewModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight( true );
        modeRightInfo.editModeDataShare( userid, formmodeid, id  );
    }
    public static String getBjdwxx(String CategoryGuid,String BiJiaGuid){
        String  returnStr = "";
        RecordSetDataSource dataSource = new RecordSetDataSource( "xfcs" );
            String sql = "select B_Name+'('+cast(m.UnitPrice as varchar)+')' as bjdwxx " +
                    "from Business_Baseinfo n,T_Assct_BiJia_Result m\n" +
                    "where n.RowGuid=m.Business_BaseinfoGuid " +
                    "and  m.CategoryGuid='"+CategoryGuid+"' and m.BiJiaGuid='"+BiJiaGuid+"'";
            dataSource.executeSql(sql);
            while (dataSource.next()){
                String bjdwxx = Util.null2String(dataSource.getString("bjdwxx"));//比价单位信息
                returnStr +="".equals(returnStr)?bjdwxx:";"+bjdwxx;
            }
        return returnStr;
    }

    public static String getGuid(){
        RecordSetDataSource dataSource = new RecordSetDataSource( "xfcs" );
        String sql = "select ProcessVersionInstanceGuid from Workflow_PVI where  EndTime in (select max(date1) from (" +
                " select t.ProcessVersionInstanceGuid,max(t.EndTime) date1,max(t1.EndTime) date2 from Workflow_PVI t " +
                " left join View_WorkFlow_PVI_Initiator t1 on t.Initiator= t1.Initiator " +
                " where t1.LoginID in ('admin','test2021') and  t.ProcessVersionInstanceName like '%办公用品比价%' " +
                " and t1.ProcessVersionInstanceName='办公用品比价流程'" +
                " and t.EndTime is not null and t1.EndTime is not null" +
                " group by t.ProcessVersionInstanceGuid) A group by A.date2)";
        dataSource.executeSql( sql);
        String guids = "";
        while (dataSource.next()){
            String guid = Util.null2String(dataSource.getString("ProcessVersionInstanceGuid"));//品牌
            guids +="".equals(guids)?"'"+guid+"'":","+"'"+guid+"'";
        }
          return guids;
    }
%>